﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using Business.IManager;
using DataAccess.IService;
using DataAccess.Service;

namespace Business.Manager
{
    public class UtilityPaymentManager:IUtilityPaymentManager
    {
        IUtilityPaymentService utilityService = new UtilityPaymentService();
        IAccommodationManager accomManager = new AccommodationManager();

        private decimal waterPerPrice = 2m;
        private decimal electricityPerPrice = 2.5m;

        public String getStuDormitoryId(string stuId){
           return  accomManager.selectDormitoryId(stuId);
        }
        public Dormitory getDormitoryInform(String dormitoryId)
        {
            Dormitory dormitory= accomManager.selectDormitoryInfor(dormitoryId);
            dormitory.DormitoryId = dormitoryId;
            return dormitory;
        }



        /// <summary>
        /// 返回学生所在宿舍需要交纳的水费信息
        /// </summary>
        /// <param name="stuId"></param>
        /// <returns></returns>
        public UtilityPaymentProject getUtilityPayment(string stuId)
        {
            String dormitoryId = getStuDormitoryId(stuId);
            Dormitory dormitory = getDormitoryInform(dormitoryId);
            String date;
            if (System.DateTime.Today.Month >= 10)
            {
                date = System.DateTime.Today.Year + "" + System.DateTime.Today.Month;
            }
            else
            {
                date = System.DateTime.Today.Year + "0" + System.DateTime.Today.Month;
            }
            UtilityPaymentProject thisMonthBill = utilityService.selectUtilPaymentThisMonth(dormitory, date);
            if (object.Equals(thisMonthBill,null))
            {
                return null;
            }


            thisMonthBill.WaterPerPrice = waterPerPrice;//2;
            thisMonthBill.ElectricityPerPrice = electricityPerPrice;//2.50M;


            if (thisMonthBill == null)
            {
                return null;
            }

            if (System.DateTime.Today.Month > 10)
            {
                date = System.DateTime.Today.Year + "" + (System.DateTime.Today.Month - 1);
            }
            else
            {
                date = System.DateTime.Today.Year + "0" + (System.DateTime.Today.Month - 1);
            }
            UtilityPaymentProject lastMonthBill = utilityService.selectUtilPaymentLastMonth(dormitory, date);

            thisMonthBill.LastElecMeterNum = lastMonthBill.ElecMeterNum;
            thisMonthBill.LastWaterMeterNum = lastMonthBill.WaterMeterNum;

            thisMonthBill.WaterUsageLimit = 1 * 4;
            thisMonthBill.ElectricityUsageLimit = 2 * 4;


            thisMonthBill.Dormitory = dormitory;
            thisMonthBill.WaterUsage = thisMonthBill.WaterMeterNum - thisMonthBill.LastWaterMeterNum;
            thisMonthBill.ElectricityUsage = thisMonthBill.ElecMeterNum - thisMonthBill.LastElecMeterNum;
           
            int stuNum = thisMonthBill.Dormitory.StudentNum;

            thisMonthBill.WaterUsageBeyond = thisMonthBill.WaterUsage - thisMonthBill.WaterUsageLimit;
            if (thisMonthBill.WaterUsageBeyond < 0)
            {
                thisMonthBill.WaterUsageBeyond = 0;
                thisMonthBill.WaterBill = 0;
            }
            else
            {
                thisMonthBill.WaterBill = 2 * thisMonthBill.WaterUsageBeyond;
            }
            thisMonthBill.ElectricityUsageBeyond = thisMonthBill.ElectricityUsage - thisMonthBill.ElectricityUsageLimit;
            if (thisMonthBill.ElectricityUsageBeyond < 0)
            {
                thisMonthBill.ElectricityUsageBeyond = 0;
                thisMonthBill.ElectricityBill = 0;
            }
            else
            {
                thisMonthBill.ElectricityBill = electricityPerPrice * thisMonthBill.ElectricityUsageBeyond;
            }




            return thisMonthBill;
        }


        /*backup*/
        public UtilityPaymentProject GetUtilityPaymentInfor(string stuId)
        {
            String dormitoryId = getStuDormitoryId(stuId);
            Dormitory dormitory =getDormitoryInform(dormitoryId);
            String date;
            if (System.DateTime.Today.Month>=10)
            {
                date = System.DateTime.Today.Year + "" + System.DateTime.Today.Month;
            }
            else{
                date = System.DateTime.Today.Year + "0" + System.DateTime.Today.Month;
            }
            
            UtilityPaymentProject thisMonthBill=utilityService.selectUtilPaymentThisMonth(dormitory,date);
            if (thisMonthBill == null)
            {
                return null;
            }

            if (System.DateTime.Today.Month>10)
            {
                date = System.DateTime.Today.Year + "" + (System.DateTime.Today.Month-1);
            }
            else{
                date = System.DateTime.Today.Year + "0" + (System.DateTime.Today.Month-1);
            }
            
            UtilityPaymentProject lastMonthBill = utilityService.selectUtilPaymentLastMonth(dormitory, date);
            thisMonthBill.LastElecMeterNum = lastMonthBill.ElecMeterNum;
            thisMonthBill.LastWaterMeterNum = lastMonthBill.WaterMeterNum;



            thisMonthBill.Dormitory = dormitory;
            thisMonthBill.WaterUsage = thisMonthBill.WaterMeterNum - thisMonthBill.LastWaterMeterNum;
            thisMonthBill.ElectricityUsage = thisMonthBill.ElecMeterNum - thisMonthBill.LastElecMeterNum;
            int stuNum = thisMonthBill.Dormitory.StudentNum;
            thisMonthBill.WaterUsageLimit = 1 * 4;
            thisMonthBill.ElectricityUsageLimit = 2 * 4;
            thisMonthBill.WaterUsageBeyond = thisMonthBill.WaterUsage - thisMonthBill.WaterUsageLimit;
            if (thisMonthBill.WaterUsageBeyond < 0)
            {
                thisMonthBill.WaterUsageBeyond = 0;
                thisMonthBill.WaterBill = 0;
            }
            else
            {
                thisMonthBill.WaterBill = 2 * thisMonthBill.WaterUsageBeyond;
            }
            thisMonthBill.ElectricityUsageBeyond = thisMonthBill.ElectricityUsage - thisMonthBill.ElectricityUsageLimit;
            if (thisMonthBill.ElectricityUsageBeyond<0)
            {
                thisMonthBill.ElectricityUsageBeyond = 0;
                thisMonthBill.ElectricityBill = 0;
            }
            else{
                thisMonthBill.ElectricityBill = electricityPerPrice * thisMonthBill.ElectricityUsageBeyond;
            }
            thisMonthBill.WaterPerPrice = waterPerPrice;//2;
            thisMonthBill.ElectricityPerPrice = electricityPerPrice;//2.50M;
            return thisMonthBill;
        }


        /*backup*/
        public UtilityPaymentProject GetUtilityPaymentInforByDorId(String dormitoryId)
        {
            Dormitory dormitory = getDormitoryInform(dormitoryId);
            String date;
            if (System.DateTime.Today.Month >= 10)
            {
                date = System.DateTime.Today.Year + "" + System.DateTime.Today.Month;
            }
            else
            {
                date = System.DateTime.Today.Year + "0" + System.DateTime.Today.Month;
            }

            UtilityPaymentProject thisMonthBill = utilityService.selectUtilPaymentThisMonth(dormitory, date);
            if (thisMonthBill == null)
            {
                return null;
            }

            if (System.DateTime.Today.Month > 10)
            {
                date = System.DateTime.Today.Year + "" + (System.DateTime.Today.Month - 1);
            }
            else
            {
                date = System.DateTime.Today.Year + "0" + (System.DateTime.Today.Month - 1);
            }

            UtilityPaymentProject lastMonthBill = utilityService.selectUtilPaymentLastMonth(dormitory, date);
            thisMonthBill.LastElecMeterNum = lastMonthBill.ElecMeterNum;
            thisMonthBill.LastWaterMeterNum = lastMonthBill.WaterMeterNum;
            thisMonthBill.Dormitory = dormitory;
            thisMonthBill.WaterUsage = thisMonthBill.WaterMeterNum - thisMonthBill.LastWaterMeterNum;
            thisMonthBill.ElectricityUsage = thisMonthBill.ElecMeterNum - thisMonthBill.LastElecMeterNum;
            int stuNum = thisMonthBill.Dormitory.StudentNum;
            thisMonthBill.WaterUsageLimit = 1 * 4;
            thisMonthBill.ElectricityUsageLimit = 2 * 4;
            thisMonthBill.WaterUsageBeyond = thisMonthBill.WaterUsage - thisMonthBill.WaterUsageLimit;
            if (thisMonthBill.WaterUsageBeyond < 0)
            {
                thisMonthBill.WaterUsageBeyond = 0;
                thisMonthBill.WaterBill = 0;
            }
            else
            {
                thisMonthBill.WaterBill = 2 * thisMonthBill.WaterUsageBeyond;
            }
            thisMonthBill.ElectricityUsageBeyond = thisMonthBill.ElectricityUsage - thisMonthBill.ElectricityUsageLimit;
            if (thisMonthBill.ElectricityUsageBeyond < 0)
            {
                thisMonthBill.ElectricityUsageBeyond = 0;
                thisMonthBill.ElectricityBill = 0;
            }
            else
            {
                thisMonthBill.ElectricityBill = 1 * thisMonthBill.ElectricityPerPrice;
            }
            thisMonthBill.WaterPerPrice = 2;
            thisMonthBill.ElectricityPerPrice = 2.50M;
            return thisMonthBill;
        }





        public bool geneUtilityPayment(List<UtilityPaymentProject> projects)
        {
            return utilityService.insertUtilityPayment(projects);
        }
    }
}
